home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte15 / cpolyrgn.c < prev    next >
C/C++ Source or Header  |  1995-12-14  |  6KB  |  187 lines

  1.  
  2. #include <windows.h>  
  3. #include "cpolyrgn.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "CreatePolyPolygonRgn()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadIcon(wcex.hInstance, "SMALL");
  99.             
  100.    return RegisterClassEx( &wcex );
  101. }
  102.  
  103.  
  104. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  105. {
  106.    switch( uMsg )
  107.    {
  108.       case WM_COMMAND :
  109.               switch( LOWORD( wParam ) )
  110.               {
  111.                  case IDM_TEST :
  112.                         {
  113.                            HDC   hDC;
  114.                            HRGN  hRgn;
  115.                            POINT ptList[7];
  116.                            int   nVertexCount[2];
  117.                       
  118.                            hDC     = GetDC( hWnd );
  119.  
  120.                            // Initialize values.
  121.                            //...................
  122.                            ptList[0].x = 60;  ptList[0].y = 10;
  123.                            ptList[1].x = 10;  ptList[1].y = 60;
  124.                            ptList[2].x = 110; ptList[2].y = 60;
  125.                            ptList[3].x = 160; ptList[3].y = 60;
  126.                            ptList[4].x = 160; ptList[4].y = 160;
  127.                            ptList[5].x = 60;  ptList[5].y = 160;
  128.                            ptList[6].x = 60;  ptList[6].y = 60;
  129.  
  130.                            nVertexCount[0] = 3;
  131.                            nVertexCount[1] = 4;
  132.  
  133.                            // Create region and fill it.
  134.                            //...........................
  135.                            hRgn = CreatePolyPolygonRgn( ptList, nVertexCount, 2, WINDING );
  136.                            FillRgn( hDC, hRgn, (HBRUSH)GetStockObject(BLACK_BRUSH) );
  137.  
  138.                            DeleteObject( hRgn );
  139.                            ReleaseDC( hWnd, hDC );
  140.                         }
  141.                         break;
  142.  
  143.                  case IDM_ABOUT :
  144.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  145.                         break;
  146.  
  147.                  case IDM_EXIT :
  148.                         DestroyWindow( hWnd );
  149.                         break;
  150.               }
  151.               break;
  152.       
  153.       case WM_DESTROY :
  154.               PostQuitMessage(0);
  155.               break;
  156.  
  157.       default :
  158.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  159.    }
  160.  
  161.    return( 0L );
  162. }
  163.  
  164.  
  165. LRESULT CALLBACK About( HWND hDlg,           
  166.                         UINT message,        
  167.                         WPARAM wParam,       
  168.                         LPARAM lParam)
  169. {
  170.    switch (message) 
  171.    {
  172.        case WM_INITDIALOG: 
  173.                return (TRUE);
  174.  
  175.        case WM_COMMAND:                              
  176.                if (   LOWORD(wParam) == IDOK         
  177.                    || LOWORD(wParam) == IDCANCEL)    
  178.                {
  179.                        EndDialog(hDlg, TRUE);        
  180.                        return (TRUE);
  181.                }
  182.                break;
  183.    }
  184.  
  185.    return (FALSE); 
  186. }
  187.